// view/index/addyuanliao/addyuanliao.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    buttonClicked: false,
    yuming: yuming,
    heights: "",
    title: '添加疫情防控',
    state: '',
    jinhuonumlist: [{
      name: '阴性'
    }, {
      name: '阳性'
    }], //进货数量
    jinhuonumindex: 0, //进货数量
    numdanindex: 0, //数量单位
    numdanlist: [{
      name: '36.1'
    }, {
      name: '36.2'
    }], //数量单位
    fazlist: [{
      name: '雨林区发证机关'
    }], //  供应商
    fazindex: 0, // 供应商
    xukejylblist: [{
      name: '食品'
    }], //原料类型
    xukejylbindex: 0, //原料类型
    date: '请选择', //开始
    dates: '请选择', //结束
    gongguo: '请选择', //供应商
    yltypeguo: '请选择', //原料类型
    photo: [],
    hsjc: '', //核酸检测
    jrtw: '', //今日体温
    jkm: '', //健康吗
    jctylist: [], //检测人员
    nucleinType: '', //是否过期 0不上传1上传
    jcryid: '', //人员id
    jcryindex: 0, //检测人员
    page: 1,
    jctyguo: '请选择',
    dateguo: '请选择检测时间',
    tiwenguo: '请选择',
    jinhuoguo: '请选择',
    jinghe_id: ''
  },
  // 疫情验证
  yqyz() {
    if (this.data.jctyguo == '请选择') {
      wx.showToast({
        title: '请选择检测人员',
        icon: 'none'
      })
    } else if (this.data.jinhuoguo == '请选择') {
      wx.showToast({
        title: '请选择核酸检测结果',
        icon: 'none'
      })
    } else if (this.data.tiwenguo == '请选择') {
      wx.showToast({
        title: '请选择今日体温',
        icon: 'none'
      })
    }
    // else if (this.data.dateguo == '请选择检测时间') {
    //   wx.showToast({
    //     title: '请选择检测时间',
    //     icon: 'none'
    //   })
    // }
    // else if (this.data.nucleinType == 1) {
    //   if (this.data.hsjc == '') {
    //     wx.showToast({
    //       title: '请上传检测报告',
    //       icon: 'none'
    //     })
    //   } else {
    //     if (this.data.jrtw == '') {
    //       wx.showToast({
    //         title: '请上传体温图',
    //         icon: 'none'
    //       })
    //     } else if (this.data.jkm == '') {
    //       wx.showToast({
    //         title: '请上传健康码',
    //         icon: 'none'
    //       })
    //     } else {
    //       this.dplist()
    //     }
    //   }
    // } 
    // else if (this.data.hsjc == '') {
    //   wx.showToast({
    //     title: '请上传核酸检测图',
    //     icon: 'none'
    //   })
    // } else if (this.data.jrtw == '') {
    //   wx.showToast({
    //     title: '请上传体温图',
    //     icon: 'none'
    //   })
    // } else if (this.data.jkm == '') {
    //   wx.showToast({
    //     title: '请上传健康码',
    //     icon: 'none'
    //   })
    // } 
    else {
      this.dplist()
    }
  },
  // 添加疫情接口
  dplist() {
    var that = this
    that.setData({
      buttonClicked: true
    })
    if(that.data.wuhsjc == undefined){
      that.data.wuhsjc = ''
    }
    if(that.data.wujrtw == undefined){
      that.data.wujrtw = ''
    }
    if(that.data.wujkm == undefined){
      that.data.wujkm = ''
    }
    if(that.data.nucleinType == undefined){
      that.data.nucleinType = ''
    }
    wx.showModal({
      title: '确定是最新的核酸检测吗？',
      cancelText: '否',
      confirmText: '是',
      success: function (res) {
        if (res.confirm) {
          that.myRequest(yuming + 'shop/shopAddKilllog', {
            shopId: wx.getStorageSync('shopId'),
            userId: that.data.jcryid,
            killName: that.data.jctyguo,
            killNuclein: that.data.jinhuoguo,
            killTiwen: that.data.tiwenguo,
            // nucleinTime: that.data.dateguo,
            nucleinImage: that.data.wuhsjc,
            killTiwenimg: that.data.wujrtw,
            killLvma: that.data.wujkm,
            nucleinType: that.data.nucleinType
          }, "POST").then(res => {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
            if (res.data.code == 200) {
              setTimeout(function () {
                wx.navigateBack({
                  delta: 0
                })
              }, 1000)
            } else {
              that.setData({
                buttonClicked: false
              })
            }
          })
        } else {
          console.log("否")
          that.setData({
            hsjc: ''
          })
        }
      }
    })
  },
  // 列表
  cylist() {
    var that = this
    this.myRequest(yuming + 'shop/shopSelectStaff', {
      page: that.data.page,
      shopId: wx.getStorageSync('shopId')
    }, 'POST').then(res => {
      that.setData({
        jctylist: res.data.result,
      })
      console.log(that.data.jctylist)
    })
  },
  // 生成二维码
  sheng() {
    wx.navigateTo({
      url: '/my_other/xinxilr/xinxilr',
    })
  },
  chuan(e) {
    var aaa = e.currentTarget.dataset.aaa //店头营业执照
    var that = this;
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file',1).then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        if (aaa == 1) {
          that.setData({
            hsjc: urls,
            wuhsjc: data.imgUrl
          });
        }
        if (aaa == 2) {
          that.setData({
            jrtw: urls,
            wujrtw: data.imgUrl
          });
        }
        if (aaa == 3) {
          that.setData({
            jkm: urls,
            wujkm: data.imgUrl
          });
        }
      })
    })
    // var aaa = e.currentTarget.dataset.aaa
    // var that = this;
    // wx.chooseImage({
    //   success: function (res) {
    //     var tempFilePaths = res.tempFilePaths
    //     wx.uploadFile({
    //       url: yuming + 'Besall/Upload',
    //       filePath: tempFilePaths[0],
    //       name: "file",
    //       formData: {

    //       },
    //       success(res) {
    //         var data = JSON.parse(res.data)
    //         wx.showToast({
    //           title: data.msg,
    //           icon: 'none'
    //         })
    //         var urls = data.httpUrl + data.imgUrl
    //         if (aaa == 1) {
    //           that.setData({
    //             hsjc: urls,
    //             wuhsjc: data.imgUrl
    //           });
    //         }
    //         if (aaa == 2) {
    //           that.setData({
    //             jrtw: urls,
    //             wujrtw: data.imgUrl
    //           });
    //         }
    //         if (aaa == 3) {
    //           that.setData({
    //             jkm: urls,
    //             wujkm: data.imgUrl
    //           });
    //         }
    //       }
    //     })
    //   }
    // })
  },
  // 选择开始时间
  bindDateChange: function (e) {
    this.setData({
      dateguo: e.detail.value
    })
  },
  // 检测人员
  jcry(e) {
    let index = e.detail.value
    this.setData({
      jctyguo: this.data.jctylist[e.detail.value].Name,
      jcryid: this.data.jctylist[e.detail.value].id,
      nucleinType: this.data.jctylist[e.detail.value].nucleinType,
    });
    // if(this.data.nucleinType == 0){
    //   this.setData({
    //     hsjc: this.data.jctylist[e.detail.value].nucleinImage,
    //   })
    // }else{
    //   this.setData({
    //     hsjc: '',
    //   })
    // }
  },
  //点击放大
  fangda(e) {
    var img = e.currentTarget.dataset.img
    var that = this
    var aaa = e.currentTarget.dataset.aaa
    wx.showModal({
      title: '您是想实现哪种操作',
      cancelText: '放大',
      confirmText: '重新上传',
      success: function (res) {
        if (res.cancel) {
          console.log("放大")
          wx.previewImage({
            urls: [img]
          })
        } else {
          that.myChooseImage(1).then(res => {
            const tempFilePaths = res.tempFilePaths
            that.myUploadImg(tempFilePaths[0], 'file').then(res => {
              var data = JSON.parse(res.data)
              wx.showToast({
                title: data.msg,
                icon: 'none'
              })
              var urls = data.httpUrl + data.imgUrl
              if (aaa == 1) {
                that.setData({
                  hsjc: urls,
                  wuhsjc: data.imgUrl
                });
              }
              if (aaa == 2) {
                that.setData({
                  jrtw: urls,
                  wujrtw: data.imgUrl
                });
              }
              if (aaa == 3) {
                that.setData({
                  jkm: urls,
                  wujkm: data.imgUrl
                });
              }
            })
          })
        }
      }
    })
  },
  // 检测结果
  jinhuonum(e) {
    let index = e.detail.value
    this.setData({
      jinhuoguo: this.data.jinhuonumlist[index].name,
    });
  },
  // 今日体温
  numdan(e) {
    let index = e.detail.value
    this.setData({
      tiwenguo: this.data.numdanlist[index],
    });
  },
  a() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'shop/shopKill_prompting', {
      shopId: wx.getStorageSync('shopId')
    }, 'POST').then(res => {
      wx.hideLoading({
        success: (res) => {},
      })
      if (res.data.code == 200) {
        this.setData({
          nr2: res.data.prompthat.replace(/(\/hr|\/n)/gi, function ($0, $1) {
            return {
              "/hr": '&ensp;',
              "/n": '\n'
            } [$1];
          }),
          nr1: res.data.prompting.replace(/(\/hr|\/n)/gi, function ($0, $1) {
            return {
              "/hr": '&ensp;',
              "/n": '\n'
            } [$1];
          })
        })
        console.log(this.data.nr1);
        console.log(this.data.nr2);
      }
      this.setData({
        a: res.data.data
      })
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var test = new Date();
    var year = test.getFullYear();
    var month = test.getMonth() + 1
    var ri = test.getDate()
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      state: options.state,
      jinghe_id: wx.getStorageSync('jinghe_id')
      // date: year + "-" + month + "-" + ri
    })
    if (this.data.state == 0) {
      this.setData({
        title: '添加原料'
      })
    }
    if (this.data.state == 1) {
      this.setData({
        title: '添加供应商'
      })
    }
    this.cylist()

    var arsr = [];
    var star = 35.0;
    for (let i = 0; i < 100; i++) {
      star += 0.1;
      arsr[i] = returnFloat(star);
    }
    this.setData({
      numdanlist: arsr
    })

    function returnFloat(value) {
      var value = Math.round(parseFloat(value) * 100) / 100;
      var xsd = value.toString().split(".");
      if (xsd.length == 1) {
        value = value.toString() + ".0";
        return value;
      }
      if (xsd.length > 1) {
        if (xsd[1].length < 2) {
          value = value.toString();
        }
        return value;
      }
    }
    this.a();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})